package com.sigmatrix.h5.web.controller.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sigmatrix.h5.entity.v2.DisTerminalShop;
import com.sigmatrix.h5.entity.v2.DisTerminalShopDetail;
import com.sigmatrix.h5.service.AdminAreaService;
import com.sigmatrix.h5.service.db.DisTerminalShopDetailDBservice;
import com.sigmatrix.h5.service.db.DisTerminalShopDBService;
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.mode.SigmatrixLocationWrapper;

import io.swagger.annotations.ApiOperation;

/**
 * 定位功能接口
 * @author wei.wang
 *
 */
@RestController
@RequestMapping("/util/location")
public class LocationController extends BaseController {

	@Autowired
	private AdminAreaService adminAreaService;

	@Autowired
	private DisTerminalShopDBService disTerminalShopImpl;

	@Autowired
	private DisTerminalShopDetailDBservice disTerminalShopDetailImpl;

	@ApiOperation("门店重新定位")
	@GetMapping("/shop/relocation/esecode/{esecode}/page/{page}/pageSize/{pageSize}")
	public void reload(@PathVariable String esecode,@PathVariable Integer page,@PathVariable Integer pageSize) {
		String tablePrefix = "_" + esecode;
//		List<DisTerminalShop> shops = disTerminalShopImpl.selectAll(tablePrefix);
		DisTerminalShop shopParam = new DisTerminalShop();
		shopParam.setTablePrefix("_"+esecode);
		Page<DisTerminalShop> selectPage = disTerminalShopImpl.selectPage(new Page<DisTerminalShop>(page, pageSize), new EntityWrapper<>(shopParam));
		List<DisTerminalShop> shops = selectPage.getRecords();
		for (DisTerminalShop tShop : shops) {

			if (StringUtils.isEmpty(tShop.getLat()) || StringUtils.isEmpty(tShop.getLng())) {
				continue;
			}
			SigmatrixLocationWrapper location = null;
			try {
				long start = System.currentTimeMillis();
				location = adminAreaService.getLocation(tShop.getLat(), tShop.getLng());
				System.err.println("adminAreaService.getLocation: "+(System.currentTimeMillis()-start));
			} catch (Exception e) {
				continue;
			}
			String addressL = location.getAddress();
			Integer provinceL = location.getProvince();
			Integer cityL = location.getCity();
			Integer distictL = location.getCitySubOrdinate();
			if (location == null || location.getProvince() == null || location.getAddress() == null
					|| location.getCity() == null) {
				continue;
			}
			Integer province = tShop.getProvince();
			Integer city = tShop.getCity();
			String address = tShop.getAddress();
			// 如果和原来的值相等 --不更新
			if (addressL.equals(address) && cityL.equals(city) && provinceL.equals(province)) {
				Integer distict = tShop.getCitySubOrdinate();
				if (distictL == null && distict == null) {
					continue;
				}
				if (distictL != null && distict != null && distictL.equals(distict)) {
					continue;
				}
			}

			// 更新DisTerminalShop
			DisTerminalShop shop = new DisTerminalShop();
			shop.setTablePrefix(tablePrefix);
			shop.setAddress(addressL);
			shop.setProvince(provinceL);
			shop.setCity(cityL);
			shop.setCitySubOrdinate(distictL);
			shop.setId(tShop.getId());
			long start = System.currentTimeMillis();
			int updateLocationById = disTerminalShopImpl.updateLocationById(shop);
			System.err.println("disTerminalShopImpl.updateLocationById: "+(System.currentTimeMillis()-start));
			// 更新DisTerminalShopDetail
			DisTerminalShopDetail detail = new DisTerminalShopDetail();
			detail.setTablePrefix("_"+esecode);
			detail.setAddress(addressL);
			detail.setProvince(provinceL);
			detail.setCity(cityL);
			detail.setCitySubOrdinate(distictL);
			detail.setTerminalShopId(tShop.getId().intValue());
			long start2 = System.currentTimeMillis();
			disTerminalShopDetailImpl.updateLocationByShopId(detail);
			System.err.println("disTerminalShopDetailImpl.updateLocationByShopId: "+(System.currentTimeMillis()-start2));
		}

	}
}
